package com.ternnetwork.batch.job;
import javax.annotation.Resource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ternnetwork.batch.model.TestUser;


/**
 * spring-batch 作业配置
 * @author xuwenfeng
 *
 */
@Configuration
public class TestUserJob {
	
	@Autowired
	public JobBuilderFactory jobBuilderFactory;
	
	
	@Autowired
	public StepBuilderFactory stepBuilderFactory;
	
	
	@Resource
	private ItemReader<TestUser> testUserMultiResourceItemReader;
	
	
	@Resource
	private ItemReader<TestUser> testUserJpaPagingItemReader;
	
	
	
	@Resource
	private ItemProcessor<TestUser, TestUser> testUserProcessor;
	
	
	@Resource
	private ItemWriter<TestUser> testUserJpaItemWriter;
	
		
	
	@Resource
	private ItemWriter<TestUser> testUserMultiResourceItemWriter;
	
	
	@Resource
	private ItemWriter<TestUser> testUserExcelItemWriter;
	
	
	
	@Bean
	public Job testUserJobjob(){
		//testUserJob要与
		return jobBuilderFactory.get("testUserJob")
				 .start(fileToDbStep())
				 .build();
	}
	
	
	/**
	 * 从文件导入到数据库
	 * @return
	 */
	@Bean
	public Step fileToDbStep(){
		return stepBuilderFactory.get("fileToDbStep")
				.<TestUser, TestUser>chunk(1)
				//设置读取器
				.reader(testUserMultiResourceItemReader)
				//设置处理器
				.processor(testUserProcessor)
				//设置写入器
				.writer(testUserJpaItemWriter)
				//完成后可重启
				.allowStartIfComplete(true)
				.build();
	}
	
	
	

}
